package cn.xluobo.business.wechat.miniapp.base.service;

import cn.xluobo.business.sys.admin.repo.model.SysDept;
import cn.xluobo.business.sys.admin.repo.model.SysRole;
import cn.xluobo.business.sys.admin.repo.model.SysTenant;
import cn.xluobo.business.sys.admin.repo.model.SysUser;
import cn.xluobo.business.sys.admin.service.*;
import cn.xluobo.business.sys.staff.repo.model.SysStaff;
import cn.xluobo.business.sys.staff.service.ISysStaffService;
import cn.xluobo.business.wechat.miniapp.base.domain.resp.RespBindUserDetailInfo;
import cn.xluobo.business.wechat.miniapp.base.domain.resp.RespBindUserInfo;
import cn.xluobo.business.wechat.user.repo.model.WechatUserInfo;
import cn.xluobo.business.wechat.user.service.IWechatUserInfoService;
import cn.xluobo.config.login.LoginUser;
import cn.xluobo.utils.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：KingLong
 * @date ：Created in 2020/10/22 08:57
 */
@Service
@Slf4j
public class WechatMiniUserService {

    @Autowired
    private ISysStaffService staffService;
    @Autowired
    private ISysTenantService tenantService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private BusinessSysUserService businessSysUserService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IWechatUserInfoService wechatUserInfoService;

    /**
     * 获取绑定用户信息
     *
     * @return
     */
    public RespBindUserInfo getBindUserInfo() {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        String name = loginUser.getName();

        Boolean hadBind = true;
        SysUser sysUser = sysUserService.getById(loginUser.getUserId());
        // 未绑定
        if (null == sysUser.getWechatUserId()) {
            hadBind = false;
        }
        WechatUserInfo wechatUserInfo = wechatUserInfoService.getById(sysUser.getWechatUserId());
        if (null == wechatUserInfo) {
            hadBind = false;
        }

        if(sysUser.getUserType() == 1){
            return RespBindUserInfo.builder()
                    .name(name)
                    .username(loginUser.getUsername())
                    .hadBind(hadBind)
                    .sysUser(sysUser)
                    .build();
        } else {
            // 当前租户信息
            String nowTenantId = loginUser.getNowTenantId();
            SysTenant tenant = tenantService.getById(nowTenantId);
            List<SysTenant> userTenantList = tenantService.selectUserTenantList(loginUser.getUserId(), null);
            userTenantList.forEach(item -> {
                if (item.getTenantId().equals(nowTenantId)) {
                    item.setNowTenant(true);
                }
            });


            // 当前校区
            Long nowDeptId = loginUser.getDeptId();
            SysDept nowDept = new SysDept();
            if (null != nowDeptId) {
                nowDept = deptService.getById(nowDeptId);
            } else {
                nowDept.setDeptName("全部校区");
            }

            List<SysDept> userCampusList = businessSysUserService.userCampusList();
            userCampusList.forEach(item -> {
                if (null == nowDeptId && null == item.getDeptId()) {
                    item.setNowDept(true);
                } else if (null != item.getDeptId() && item.getDeptId().equals(nowDeptId)) {
                    item.setNowDept(true);
                }
            });

            List<SysRole> roleList = roleService.selectUserRoleList(loginUser.getUserId(), loginUser.getNowTenantId());
            List<String> roles = roleList.stream().map(SysRole::getRoleCode).collect(Collectors.toList());

            return RespBindUserInfo.builder()
                    .name(name)
                    .username(loginUser.getUsername())
                    .tenantName(tenant.getTenantName())
                    .tenantList(userTenantList)
                    .campusName(nowDept.getDeptName())
                    .campusList(userCampusList)
                    .roles(roles)
                    .hadBind(hadBind)
                    .sysUser(sysUser)
                    .build();
        }
    }

    /**
     * 绑定用户详细信息
     *
     * @return
     */
    public RespBindUserDetailInfo getBindUserDetailInfo() {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        Long staffId = loginUser.getStaffId();
        if (null == staffId) {
            return null;
        }
        SysStaff sysStaff = staffService.getById(staffId);

        RespBindUserDetailInfo userDetailInfo = RespBindUserDetailInfo.builder().build();
        BeanUtils.copyProperties(sysStaff, userDetailInfo);
        return userDetailInfo;
    }
}
